package com.application.Tree;

import it.unipr.ce.dsg.s2p.sip.Address;

import java.util.ArrayList;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import ReadWriteXML.CMember;
import ReadWriteXML.ReadXML;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PointF;
import android.os.Environment;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

import com.application.Activity.CommonUtilities;
import com.application.Activity.MESApplication;
import com.application.Activity.R;
import com.application.Activity.frmTreeNodeActivity;
import com.application.Controller.NetworkController;
import com.application.Controller.ReadWriteFileController;
import com.application.Network.Protocol;
import com.application.Network.WriteXMLThread;
import com.application.PhanQuyen.Quyen;
import com.application.outdoor.InvoikeServices.CayThaoLuanInvoike;

public class BoardView extends SurfaceView implements SurfaceHolder.Callback {
	public static float horizontal = 100;
	public static float vertical = 50;
	public static int T_MOVE = 1;
	public static int T_NEW = 2;
	public static int T_UPDATE = 3;
	public static int T_MOVE_MENU = 4;
	public static String treePath=Environment.getExternalStorageDirectory()+"/"+"tree.xml";
	public static String DeletedNodesPath=Environment.getExternalStorageDirectory()+"/"+"DeletedNodes.xml";
	public  boolean isActive=false;
	public static final float ZOOM_MAX=2f;
	public static final float ZOOM_MIN=0.5f;
	public static final float ZOOM_STEP=0.2f;
	public BoardLoopThread loopThread;
	private  TreeNode tree;
	public int control = T_MOVE;
	float xDown = -1, yDown = -1, xUp = -1, yUp = -1, x = -1, y = -1;
	private TreeNode currentNode=null;
	long downTime = 0, longTime = 0, upTime = 0, tempTime = 0;
	
	private MODE mode=MODE.DRAG;
	float oldSpacing=1;
	float newSpacing=1;
	PointF mildePoint;
	float scale = 1.0f;	
	TreeMenu menu;
	FocusRect focusRect;
	String ghiChu="";
	private TreeMenu menuZoom;
	public WriteXMLThread writeXmlThread;
	public boolean stopDiscuss = false;
	public int userColor = 0;
	
	//private long vs;
	private long pID;
	private float hTest=-1;
	public BoardView(Context context) {
		super(context);
		init();
	}

	public BoardView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public BoardView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {
		isActive = true;
		int h = MESApplication.getHeight();
		int w = MESApplication.getWidth();
		getHolder().addCallback(this);
		loopThread = new BoardLoopThread(this);
		mildePoint = new PointF(w / 2, h / 2);
		mildePoint.x = 0;
		mildePoint.y = 0;
		//tạo menu
		menu = new TreeMenu();
		menuZoom=new TreeMenu();
		
		if(!ReadWriteFileController.isFile(treePath)){
			Log.d("Phu","Boardview-init...create Root");
			tree=createRoot();
			tree.autoArrange();
		}
		else{
			Log.d("Phu","Boardview-init...doc tu file vào nodes");
			readXml(treePath);			
			tree.autoArrange();
		}
		tree.expand(true);
		this.control = T_MOVE;
		menu.createEditMenu();
		menuZoom.createZoomMenu();
		moveCenter();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		if (canvas == null)
			return;
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setStyle(Style.STROKE);
		paint.setStrokeWidth(1);
		paint.setColor(Color.RED);
		//Vẽ màu nền
		canvas.drawColor(Color.rgb(183, 222, 232));
		//Vẽ menu zoom
		menuZoom.onDraw(canvas);	
	
		if(currentNode!=null){
			//Vẽ menu edit
			menu.onDraw(canvas);
		}
		
		//Scale canvas
		canvas.scale(scale, scale, mildePoint.x, mildePoint.y);
		
		tree.onDraw(canvas);

		if (focusRect != null) {
			paint.setStrokeWidth(2);
			focusRect.onDraw(canvas);
		}
		canvas.restore();
		
	}


	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int action=event.getAction()&MotionEvent.ACTION_MASK;
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			touchDown(event.getX(), event.getY());
			mode=MODE.DRAG;
			break;
		case MotionEvent.ACTION_MOVE:
			if(mode==MODE.DRAG){
				touchMove(event.getX(), event.getY());
			}		
			break;
		case MotionEvent.ACTION_UP:
			touchUp(event.getX(), event.getY());			
			break;
			
		default:
			break;
		}
		return true;
	}

	private void touchDown(float x, float y) {	
		downTime = System.currentTimeMillis();
		control = T_MOVE;
		
		INode collistionNode=tree.getCollision(x/scale, y/scale);
		INode expandNode=tree.getExpandCollision(x/scale, y/scale);
		
		if(expandNode!=null){
			if(expandNode.getChildren().size()!=0){
				expandNode.expand(!expandNode.expand.isExpand());
				tree.autoArrange();
				focusRect=new FocusRect(currentNode);
				ghiChu="expandNode ! =null";
			}
			
		}
		if (collistionNode != null) {
			currentNode = (TreeNode) collistionNode;
			focusRect = new FocusRect(currentNode);
			ghiChu = (currentNode.getId() + " "+(currentNode.user.equals(NetworkController._peer.getAccount().get_Username())));
			menu.setShowed(true);
			menuZoom.setShowed(false);
			if(NetworkController.isOutDoor()){
				frmTreeNodeActivity.userNameAvatar=currentNode.getUser();
				new Handler().post(frmTreeNodeActivity.raSetAvatarPosition);
			}
		} else {
			menuZoom.setShowed(true);
			if (menu.isShowed()) {				
				int mnuID = menu.getCollisionItemId(x, y);
				switch (mnuID) {
				case TreeMenu.ADD:
					Quyen myRule=NetworkController._peer.getAccount().getRuleById(16);
					if(myRule!=null && myRule.getSend())
						addNode();
					else
						NetworkController.ShowMessageNoticeOnTree(Protocol.NON_RULE_MESSAGE);
					break;
				case TreeMenu.REMOVE:
					removeNode();
					break;
				case TreeMenu.EDIT:
					updateText();
					break;
				case TreeMenu.NOTE:
					showNotes();
					break;
				case TreeMenu.DETAILS:
					showDetails();
					break;
				case TreeMenu.ARRANGE:
					control=T_MOVE_MENU;					
					break;
				case TreeMenu.MOVE:
					control=T_MOVE_MENU;
					break;
				default:
					break;
				}
				if(control==T_MOVE_MENU)
					return;
			} else if (menuZoom.isShowed()) {
				int zoomID = menuZoom.getCollisionItemId(x, y);
				switch (zoomID) {
				case TreeMenu.ZOOM_IN:
					if (this.scale < ZOOM_MAX) {
						scale += ZOOM_STEP;
					}
					break;
				case TreeMenu.ZOOM_OUT:
					if (this.scale > ZOOM_MIN) {
						scale -= ZOOM_STEP;
					}
					break;

				default:
					break;
				}
			}

			menu.setShowed(false);
			menuZoom.setShowed(true);
			focusRect=null;
		}
		this.x=x;
		this.y=y;
	}

	private void touchUp(float x, float y) {
		if (upTime - downTime > 100)
			control = T_MOVE;
		else
			control = T_NEW;
		
		if(control==T_MOVE_MENU)
			control=T_MOVE;
		tempTime = downTime;

		this.x = x;
		this.y = y;
		control = -1;
		upTime = System.currentTimeMillis();
	}

	private void touchMove(float x, float y) {
		float moveX, moveY;

		 if (control == T_MOVE) {
			moveX = x - this.x;
			moveY = y - this.y;
			tree.moveAll(moveX/scale, moveY/scale);
			if(focusRect!=null)
				focusRect.moveAll(moveX/scale, moveY/scale);
		}else if(control==T_MOVE_MENU){
			this.menu.setPosition(x-this.menu.width, y);
		}
		this.x = x;
		this.y = y;
	}

	private void addNode(){			
		Log.e("Phu","vao ham addNode");
		Quyen quyen=NetworkController._peer.getAccount().getRuleById(16);//quyen them node tren cay
		
		if(quyen==null||(quyen!=null && !quyen.getSend())){
			Log.i("Phu", "ko co quyen them node");
			MESApplication.showDialog(this.getContext(), "Thông báo", Protocol.NON_RULE_MESSAGE);
			return;
		}
		Log.i("Phu", " co quyen them node");
		final MyDialog dialog=new MyDialog(getContext(),"Tạo node mới!");			
		dialog.show();
		dialog.setOKClickListener(new MyHandlerInterface() {

			@Override
			public void onHandle(Object obj) {
				long vs = System.currentTimeMillis();
				pID = currentNode.getId();
				long mId = NetworkController.getPersonID();
				String user = NetworkController._peer.getAccount().get_Username();
				int avarta = NetworkController._peer.account.get_Avarta();
				int color = Color.BLUE;
				if(NetworkController.Color != 0){
					color = NetworkController.Color;
				}
				String title = dialog.getTextTitle();
				String text = dialog.getText();
//				int emoticon = dialog.getEmoticon();
				if (text.equals(MyDialog.defaultText))
					text = "";			
				if (NetworkController.isOutDoor()) {
					try {
						String rs=new CayThaoLuanInvoike().themNode((int)pID, CommonUtilities.idCayHienTai, CommonUtilities.nguoiDung.getTenDangNhap(), title, text);
						if(rs!=null&&rs!=""&&!rs.equals(Protocol.SERVER_ERROR)){
//							INode node = new TreeNode(Long.parseLong(rs), title, text, 0, 0, user, vs, 0, pID);
//							node.showed = currentNode.expand.isExpand();
//							// tree.version = node.getVersion();
//							currentNode.add(node);
//							tree.version = node.version;
//							tree.autoArrange();
//							focusRect = new FocusRect(node);
//							writeXML(treePath);
						}
					} catch (Exception e) {
						Log.e(Protocol.TAG, "BoardView.addNode() "+e.getMessage());
						MESApplication.showDialog(context, "Lỗi hệ thống", "Không thể thêm ý kiến!");
						e.printStackTrace();
					}
				} else {
					INode node = new TreeNode(mId, title, text, 0, 0, user, vs, 0, pID,avarta,color);
					node.showed = currentNode.expand.isExpand();
					// tree.version = node.getVersion();
					currentNode.add(node);
					tree.version = node.version;
					tree.autoArrange();
					focusRect = new FocusRect(node);
					writeXML(treePath);
//					Log.d("Phu","Boardview-onDraw...avarta.."+currentNode.getAvarta() );
					// Gửi thông tin cho các thành viên
					ArrayList<CMember> members = ReadWriteFileController
							.readMember(ReadWriteFileController.MEMBER_PATH);
					for (CMember m : members) {
						NetworkController.sendAddTreeNode(new Address(m.get_ip()), currentNode.getId(), node);

					}
				}
				Log.e("Phu","send them node xong board");
			}

		});
		dialog.setCancelClickListener(new MyHandlerInterface() {
			
			@Override
			public void onHandle(Object obj) {
				//tree.remove(newNode);
			}
		});
		Log.e("Phu","da them node xong boardview");
	}
	private void removeNode(){
		Log.e("Phu","vao ham removeNode");
		// Kiểm tra có quyền xóa node ko?
		Log.i("Phu","current node="+currentNode.getUser());
		Log.i("Phu","my username="+NetworkController._peer.getAccount().get_Username());
		Log.i("Phu","quyen="+NetworkController._peer.getAccount().getRuleById(17));
		boolean delete = true;
		Quyen q = NetworkController._peer.getAccount().getRuleById(Protocol.R_XOA_NODE);//quyền xóa node trên cây
		Quyen q1=NetworkController._peer.getAccount().getRuleById(Protocol.R_DUNG_CUOC_HOP);//quyền này là dừng 
																				//cuộc họp của người giám sát
		String loi = "";
		if(q1!=null && (q1.getSend()==true)){
			delete=true;
		}
		else if (q == null || (q != null && q.getSend() == false)) {
			delete = false;
			loi = "Bạn không có quyền xóa!";
		}else{
			//Nếu không phải node của mình thì ko đc xóa
			
			if(!currentNode.getUser().equals(NetworkController._peer.getAccount().get_Username())){
				Log.v("Phu","ko the xoa node cua ngkhac");
				delete=false;
				loi="Không thể xóa ý kiến của người khác!";
			}else if(currentNode.haveOtherChildren(NetworkController._peer.getAccount().get_Username())){
				delete=false;
				Log.v("Phu","ko the xoa node vi co comment");
				loi = "Không thể xóa khi có ý kiến bình luận của người khác!";
			}else{
				Log.v("Phu","xoa node ok");
				delete=true;
			}
		}

		if (delete) {
			
			AlertDialog.Builder buidler=new Builder(getContext());
			buidler.setTitle("Thông báo").setIcon(R.drawable.messagebox_warning);
			buidler.setMessage("Bạn có muốn xóa node này không?");
			buidler.setPositiveButton("Có", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					
					Toast.makeText(getContext(), "Xóa node: " + currentNode.getId(), 1).show();
					if (NetworkController.isOutDoor()) {
						try {
							String rs = new CayThaoLuanInvoike().xoaNode((int) currentNode.getId(),
									CommonUtilities.idCayHienTai,CommonUtilities.nguoiDung.getTenDangNhap());

						} catch (Exception e) {
							Log.d("Phu", "CayThaoLuanInvoike.xoaNode" + e.getMessage());
							e.printStackTrace();
						}
					} else {
						long vs = System.currentTimeMillis();
						tree.remove(currentNode);
						tree.version = vs;
						currentNode.version = vs;
						writeDeleteXML(currentNode.id, currentNode.version);
						writeXML(treePath);
						// Gửi thông tin cho các thành viên
						ArrayList<CMember> members = ReadWriteFileController
								.readMember(ReadWriteFileController.MEMBER_PATH);
						for (CMember m : members) {
							NetworkController.sendDeleteTreeNode(new Address(m.get_ip()), currentNode);

						}
					}
					currentNode = null;
					focusRect = null;
					tree.autoArrange();
				}
				
			});
			buidler.setNegativeButton("Không", null);
			buidler.show();
			
		} else {			
			MESApplication.showDialog(this.getContext(), "Thông báo", loi);
			
		}
	}
	private void updateText(){
		Log.e("Phu","vao ham updateNode");
		
		Log.v("Phu","update: current node user: "+currentNode.getUser());
		Log.v("Phu","update: my user: "+NetworkController._peer.getAccount().get_Username());

//		if(currentNode.getUser().equals(NetworkController._peer.getAccount().get_Username()) || currentNode.getUser().equals("")){
		if(NetworkController._peer.getAccount().get_Username().equals(currentNode.getUser())){
		final MyDialog dialog=new MyDialog(getContext(),"Cập nhật!");	
		dialog.setFlagEmoticon(true);
		dialog.setEmoticon(currentNode.avarta);
		dialog.setTextTitle(currentNode.getTitle());
		dialog.setText(currentNode.getText());
		dialog.show();
		dialog.setOKClickListener(new MyHandlerInterface() {
			
			@Override
			
			public void onHandle(Object obj) {		
					if (NetworkController.isOutDoor()) {
						try {
							new CayThaoLuanInvoike().capNhatNode((int) currentNode.getId(), CommonUtilities.idCayHienTai,
									dialog.getTextTitle(), dialog.getText(),CommonUtilities.nguoiDung.getTenDangNhap());
						} catch (Exception e) {
							Log.e(Protocol.TAG,"boardview.updateText "+e.getMessage());
							MESApplication.showDialog(context, "Lỗi hệ thống", "Không thể cập nhật ý kiến!");
							e.printStackTrace();
						}
					} else {
						currentNode.setUser(NetworkController._peer.getAccount().get_Username());
						currentNode.setText(dialog.getText());
						currentNode.setTitle(dialog.getTextTitle());
						currentNode.setEmoticon(dialog.getEmoticon());
						currentNode.generateSize();
						currentNode.label = 1;
						currentNode.version = System.currentTimeMillis();
						tree.version = currentNode.version;
						// currentNode.setBgColor(dialog.getColor());
						focusRect = new FocusRect(currentNode);
						tree.autoArrange();
						writeXML(treePath);
						// Gửi thông tin cho các thành viên
						ArrayList<CMember> members = ReadWriteFileController
								.readMember(ReadWriteFileController.MEMBER_PATH);
						for (CMember m : members) {
							NetworkController.sendUpdateNodeText(new Address(m.get_ip()), currentNode);

						}
					}
				}
			});
		dialog.setCancelClickListener(new MyHandlerInterface() {
			
			@Override
			public void onHandle(Object obj) {
				
			}
		});
		}else{
			MESApplication.showDialog(getContext(), "Thông báo", "Không thể cập nhật ý kiến của người khác");
		}
	}
	
	 public void updateText(INode node, String path) {
//		if (!isActive) {
//			this.readXml(path);
//			INode inode = tree.getById(node.getId());
//			if (inode != null) {
//				inode.copy(node);
//				tree.version = node.getVersion();
//				writeXML(path);
//			}
//
//		} else {
//		 	this.readXml(path);
		 synchronized (this.getHolder()) {
			 INode inode = tree.getById(node.id);
				if (inode != null) {
					inode.copy(node);
					tree.version = node.getVersion();
					focusRect=new FocusRect(inode);
					writeXML(path);
					tree.autoArrange();
					
					Log.d("An","BoardView updateText: Đã cập nhật node i="+node.id+":"+tree.getById(node.id).title);
				}
			
		}
			
//		}
 }

	private void showNotes(){
		boolean readOnly=(!currentNode.getUser().equals(NetworkController._peer.account.get_Username()));
		final NoteDialog dialog=new NoteDialog(getContext(),readOnly);
		dialog.setNote(currentNode.getText());
		
		dialog.setOKClickListener(new MyHandlerInterface() {
			
			@Override
			public void onHandle(Object obj) {
				currentNode.setText(dialog.getNote());
//				currentNode.label=1;
				currentNode.version=System.currentTimeMillis();
				tree.version=currentNode.version;
				
				// Gửi thông tin cho các thành viên
				ArrayList<CMember> members = ReadWriteFileController
						.readMember(ReadWriteFileController.MEMBER_PATH);
				for (CMember m : members) {
					NetworkController.sendUpdateNodeText(new Address(m.get_ip()),currentNode);

				}
			}
		});
		dialog.show();
	}
	private void showDetails(){
		AlertDialog.Builder buidler=new Builder(getContext());
		buidler.setTitle("Chi tiết");
		buidler.setMessage(currentNode.getUser()+":\r\n"+currentNode.getTitle());
		
		buidler.setNegativeButton("Đóng", null);
		buidler.show();	
	}
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		// this.setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
	}

	public void setTool(int tool) {
		this.control = tool;
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		// TODO Auto-generated method stub

	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		Log.d("Phu", "surfaceCreated BoardView ");
		isActive = true;
		tree.autoArrange();
//		writeXmlThread = new WriteXMLThread(this);
//		writeXmlThread.setRunning(true);
//		writeXmlThread.start();
		if (loopThread.getState() == Thread.State.TERMINATED) {
			loopThread = new BoardLoopThread(this);
			loopThread.setRunning(true);
			loopThread.start();
		} else {
			loopThread.setRunning(true);
			loopThread.start();
		}
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		isActive=false;
		Log.d("Phu", "surfaceDestroyed BoardView");
		boolean retry = true;
		loopThread.setRunning(false);
//		writeXmlThread.setRunning(false);
		while (retry) {
			try {
				loopThread.join();
				retry = false;
			} catch (InterruptedException e) {

			}
		}
	}

	public void removeNode(TreeNode node) {
		tree.remove(node);
		
	}
//	public void removeNode(TreeNode node,String path) {
//		tree.remove(node);
//		writeXML(path);
//		
//	}
	
	public void removeNodeByID(long id){	
		Log.d("An","Boardview-removeNodeByID...id: "+id);
		INode n=tree.getById(id);		
		tree.remove(n);
		
		
	}
	public void removeNodeById(INode node,String path) {		
			
//		if(isActive){
//			this.readXml(path);
			Log.d("An","Boardview-removeNodeById: isActive= " + isActive);
			tree.version = node.version;	
			removeNodeByID(node.id);
			writeXML(path);
			writeDeleteXML(node.id,node.version);
//		}else{
//			this.readXml(path);
//			this.removeNodeByID(node.id);			
//			writeXML(path);
//			writeDeleteXML(node.id,node.version);			
//		}		
		tree.autoArrange();
	}
	public void writeXML(String path){
		tree.writeXML(path);
		Log.d("Phu","Boardview-writeXML: Da ghi cay xuong file:"+path);
	}
	public void writeDeleteXML(long id, long vs){
		Log.d("An","writeDeleteXML: ghi node delete xuong file:");
		tree.WriteToDeleteFile( id, vs);
	}
	
	public void readXml(String path){
		Document doc=ReadXML.BuildXML(path);
		tree=new TreeNode();
		tree.readXML((Element)doc.getFirstChild().getFirstChild());
		Log.d("Phu", "Boardview-readXml....doc file:.." +tree.getSumChildren());
	}
	
	public void addNode(long idParent, INode node,String path){
//		this.readXml(path);
		Log.i("Phu","Vao add node trogn board");
		INode p=tree.getById(idParent);
		if(p!=null){
			p.add(node);			
			tree.version=node.version;
			writeXML(path);
			focusRect=null;
			tree.autoArrange();
		}
	}
//	public void addNode(long idParent, INode node,String path){
//		Log.d("Phu","Boardview.addNode: "+" - "+ idParent);
//		if(!isActive){
//			this.readXml(path);
//			INode p=tree.getById(idParent);
//			if(p!=null){
//				p.add(node);
//				tree.version = node.getVersion();				
//				Log.d("An","Boardview.addNode:"+ tree.getVersion());
//				writeXML(path);
//			}
//		}else{
//			addNode(idParent, node);
//			
//		}
//	
//	}
//	public void setText(long id, String text, String path){		
//		if (!isActive) {			
//			this.readXml(path);		
//			INode node = tree.getById(id);
//			if (node != null) {
//				node.setText(text);
//			}	
//			writeXML(path);
//		}else{
//			INode node = tree.getById(id);
//			if (node != null) {
//				node.setText(text);
//			}	
//		}
//	}
	public  long treeversion(){
		return tree.getVersion();
	}
	public void zoomIn(){
		tree.zoomIn();
	}
	public void zoom(float scale) {
		this.scale = scale;
	}
	public enum MODE{
		ZOOM,DRAG
	}
	public static TreeNode createRoot() {
		String title,text;
		
		title="[Node gốc]";
		text="";
		int h=MESApplication.getHeight();
		int w=MESApplication.getWidth();
		Log.d("Phu", "Boardview-createRoot....:.." );
		//TreeNode rs = new TreeNode(1, title, text, w/2, h/2, "phu",1,1,0,R.drawable.icon_space);		
		TreeNode rs = new TreeNode(1, title, text, w/2, h/2, "[Root]",1,1,0,2130837601,Color.BLACK);		
		rs.writeXML(treePath);
		return rs;
	}

	public void autoArrange() {
		tree.autoArrange();		
	}
	public void moveCenter(){
		int w=MESApplication.getWidth();
		int h=MESApplication.getHeight();
		tree.setPostion(w/2, 100);
		tree.autoArrange();
		//focusRect=new FocusRect(tree);
		
	}

	public TreeNode getTree() {
		return tree;
	}
	
	Context context = getContext();
	public Context getcontext() {
		return context;
	}
	
	
}
